package net.zjitc.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.zjitc.dto.DishDto;
import net.zjitc.entity.Category;
import net.zjitc.entity.Dish;
import net.zjitc.entity.DishFlavor;
import net.zjitc.mapper.DishMapper;
import net.zjitc.service.CategoryService;
import net.zjitc.service.DishFlavorService;
import net.zjitc.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //实现保存Dto的逻辑
        //我先去保存一个菜品数据
        this.save(dishDto);
        //再去保存多个口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //流 工厂的流水线
        List<DishFlavor> list = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(list);
    }

    @Override
    public Page<DishDto> pageWithCategoryName(Integer page, Integer pageSize, String name) {
        /*dish查询出来的分页结果是不是基本上没有问题？
         * categoryName没显示出来
         * 用了DishDto来代替Dish
         * Page<Dish> -> Page<DishDto>
         * 加入一个categoryName
         * */
        Page<Dish> dishPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.like(name != null, Dish::getName, name);
        this.page(dishPage, dishLambdaQueryWrapper);

        Page<DishDto> dishDtoPage = new Page<>();
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

        List<Dish> dishList = dishPage.getRecords();
        //接下来的目标就是，从dishList转换为dishDtoList
        //dishList和dishDtoList
        //对于每个dish对象，将其转换为DishDto，为它添加上categoryName字段

        //流式编程
        List<DishDto> dishDtoList = dishList.stream().map((item) -> {
            //定义操作流程 item dish对象
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            //如何获得当前菜品的菜品分类名，categoryName
            //已知分类id，查询分类名字
            Category category = categoryService.getById(item.getCategoryId());
            dishDto.setCategoryName(category.getName());
            return dishDto;
        }).collect(Collectors.toList());


        dishDtoPage.setRecords(dishDtoList);

        return dishDtoPage;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        //删除当前菜品在数据库中保存的口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);

        //添加当前dishDto传递过来的菜品口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor:flavors){
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }
}
